package fr.cie.blog.modeldao.hib;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import fr.cie.blog.beans.Card;
import fr.cie.blog.beans.Country;
import fr.cie.blog.domainservice.IServiceCountry;

/**
 * @author AetM
 * 
 */
public class CountryDaoImplHib extends HibernateDaoSupport implements
		IServiceCountry {

	/**
	 * Log4J the logger.
	 */
	private static final Logger LOG = Logger
			.getLogger(CountryDaoImplHib.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.cie.blog.domainservice.IServiceGeneric#save(java.lang.Object)
	 */
	@Override
	public final int save(final Country country) {
		final Session hibSession = getSessionFactory().getCurrentSession();
		final Transaction transaction = hibSession.beginTransaction();

		try {
			hibSession.saveOrUpdate(country);
			transaction.commit();

		} catch (DataAccessException e) {
			transaction.rollback();
			LOG.error("save Country error", e);

		} catch (HibernateException e) {
			transaction.rollback();
			LOG.error("save Country error", e);

		} finally {
			getSession().close();
		}
		return 1;
	}

	@Override
	public final int save(final List<Country> pCountryListToAdd) {
		final Session hibSession = getSessionFactory().getCurrentSession();
		// LOG.debug("la session is open  ? " + sessionH.isOpen());
		final Transaction transaction = hibSession.beginTransaction();
		// LOG.debug("La transaction est active ? " + tx.isActive());
		try {
			for (Country country : pCountryListToAdd) {
				// LOG.debug("add");
				hibSession.saveOrUpdate(country);
				// LOG.debug("after save.");
			}
			transaction.commit();
		} catch (DataAccessException e) {
			transaction.rollback();
			LOG.error("save countryList error", e);
		} catch (HibernateException e) {
			transaction.rollback();
			LOG.error("save countryList error", e);
		} finally {
			getSession().close();
		}
		return 1;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.cie.blog.domainservice.IServiceGeneric#update(java.lang.Object)
	 */
	@Override
	public final int update(final Country country) {
		final Session hibSession = getSessionFactory().getCurrentSession();
		final Transaction transaction = hibSession.beginTransaction();

		try {
			hibSession.saveOrUpdate(country);
			transaction.commit();

		} catch (HibernateException e) {
			transaction.rollback();
			LOG.error("update Country error", e);

		} finally {
			getSession().close();
		}
		return 1;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.cie.blog.domainservice.IServiceGeneric#delete(java.lang.Object)
	 */
	@Override
	public final int delete(final Country country) {
		final Session hibSession = getSessionFactory().getCurrentSession();
		final Transaction transaction = hibSession.beginTransaction();

		try {
			hibSession.delete(country);
			transaction.commit();

		} catch (HibernateException e) {
			transaction.rollback();
			LOG.error("delete Country error", e);

		} finally {
			getSession().close();
		}
		return 1;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.cie.blog.domainservice.IServiceGeneric#findAll()
	 */
	@Override
	public final List<Country> findAll() {
		@SuppressWarnings("unchecked")
		final List<Country> list = (List<Country>) getHibernateTemplate()
				.find("from Country");
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * fr.cie.blog.domainservice.IServiceCountry#findByCard(fr.cie.blog.beans
	 * .Card)
	 */
	@Override
	public final Country findByCard(final Card pCard) {
		final Country country = (Country) getHibernateTemplate().find(
				"from Country as c where c.id = ?",
				pCard.getCountry().getId());
		return country;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.cie.blog.domainservice.IServiceGeneric#findById(int)
	 */
	@Override
	public final Country findById(final long pIdentifier) {
		return getHibernateTemplate().get(Country.class, pIdentifier);
	}

}